package com.xiaoyao.novel.crawl;

import java.io.IOException;
import java.util.List;

import org.apache.log4j.Logger;

import com.xiaoyao.novel.parser.CommonParser;
import com.xiaoyao.novel.pojo.Book;
import com.xiaoyao.novel.pojo.Chapter;
import com.xiaoyao.novel.pojo.CrawlConfig;
import com.xiaoyao.novel.util.ClassUtil;
import com.xiaoyao.novel.util.HttpClientUtil;
import com.xiaoyao.novel.util.HttpUtil;

public class CommonCrawl extends BaseCrawl {
	static Logger logger = Logger.getLogger(CommonCrawl.class);

	private HttpUtil httpUtil = null;
	private CrawlConfig crawlConfig = new CrawlConfig();

	public CommonCrawl(CrawlConfig crawlConfig) {
		this.crawlConfig = crawlConfig;
		if (crawlConfig.getHttpType() == 1) {
			httpUtil = new HttpClientUtil(crawlConfig.getCharset());
		} else {
			httpUtil = new HttpUtil(crawlConfig.getCharset());
		}
	}
	@Override
	public void crawlBook(Book book) {
		String page = "";
		try {
			// http://www.qidian.com/Book/2988814.aspx
			if (ClassUtil.isEmpty(book.authorName)) {
				String describUrl = crawlConfig.getDescribUrl().replace("[webId]", book.webId);
				logger.info("读取简介:" + describUrl);

				page = httpUtil.getPage(describUrl);
				CommonParser.parseDescrib(page, book, crawlConfig);
			}

			String categoryUrl = crawlConfig.getCategoryUrl().replace("[webId]", book.webId);
			logger.info("读取目录:" + categoryUrl);
			page = httpUtil.getPage(categoryUrl);
			List<Chapter> list = CommonParser.parseBook(page, book, crawlConfig);
			for (int i = book.savedCount; i < list.size(); i++) {
				Chapter chapter = book.list.get(i);
				crawlChapter(chapter);
			}
		} catch (IOException e) {
			logger.error(e);
		}
	}

	@Override
	public void crawlChapter(Chapter chapter) throws IOException {
		String urlStr = chapter.chapterUrl;
		if (!urlStr.startsWith("http:")) {
			urlStr = crawlConfig.getCrawlPageHome() + urlStr;
		}
		logger.info("爬取章节 :[" + urlStr + "]");
		String page = "";
		for (int i = 0; i < 3 && ClassUtil.isEmpty(page); i++) {
			try {
				page = httpUtil.getPage(urlStr);
			} catch (Exception e) {
				logger.error(e);
			}
		}
		page = filter(page);
		CommonParser.parseChapter(page, chapter, crawlConfig);
		logger.info("内容长度:" + chapter.chapterContent.length());
	}
	@Override
	public String filter(String page) {
		page = super.filter(page);// 默认过滤
		// 各自的过滤
		page = page.replaceAll(crawlConfig.getReplaceRegex(), "");
		return page;
	}

}
